package com.example.demo.qy.serviceimpl;

import com.example.demo.common.utils.ResultUtil;
import com.example.demo.common.vo.Result;
import com.example.demo.qy.dao.UserDao;
import com.example.demo.qy.model.User;
import com.example.demo.qy.service.RoleService;
import com.example.demo.qy.service.UserService;
import com.example.demo.security.JwtTokenUtil;
import com.example.demo.security.JwtUserDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashSet;

@Service
public class UserServiceImpl implements UserService {

    private AuthenticationManager authenticationManager;
    private JwtUserDetailServiceImpl userDetailsService;
    private JwtTokenUtil jwtTokenUtil;
    private UserDao UserDao;
    private RoleService roleService;

    @Autowired
    public UserServiceImpl(RoleService roleService,AuthenticationManager authenticationManager, JwtUserDetailServiceImpl userDetailsService, JwtTokenUtil jwtTokenUtil, com.example.demo.qy.dao.UserDao userDao) {
        this.authenticationManager = authenticationManager;
        this.userDetailsService = userDetailsService;
        this.jwtTokenUtil = jwtTokenUtil;
        UserDao = userDao;
        this.roleService = roleService;
    }

    @Override
    public UserDao getRepository() {
        return UserDao;
    }

    @Override
    public Result<Object> login(String username, String password) {
        UsernamePasswordAuthenticationToken upToken = new UsernamePasswordAuthenticationToken(username, password);
        Authentication authentication = authenticationManager.authenticate(upToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        UserDetails userDetails = userDetailsService.loadUserByUsername(username);
        return new ResultUtil<>().setData(jwtTokenUtil.generateToken(userDetails));
    }

    @Override
    public Object register(User user) {
        String username = user.getUsername();
        if(null != UserDao.findByUsername(username)){
            return "用户已经存在";
        }
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String rawPassword = user.getPassword();
        user.setPassword(encoder.encode(rawPassword));
        user.setRoleSet(new HashSet<>(this.roleService.getAll()));
        UserDao.save(user);
        return "success";
    }

    @Override
    public Object refreshToken(String oldToken) {
        if (!jwtTokenUtil.isTokenExpired(oldToken)) {
            return jwtTokenUtil.refreshToken(oldToken);
        }
        return "error";
    }
}
